#include <iostream>
#include "TaskQueue.h"

using std::cout;
using std::endl;
using std::unique_lock;

TaskQueue::TaskQueue(size_t queSize)
    : _queSize{queSize}
{
}
TaskQueue::~TaskQueue()
{
}

void TaskQueue::push_back(Task *task)
{
    unique_lock<mutex> lk(_mtx);
    while (full())
    {
        _cond.wait(lk);
    }

    _tasks.push(std::unique_ptr<Task>(task));
    _cond.notify_all();
}

std::unique_ptr<Task> TaskQueue::front()
{
    unique_lock<mutex> lk(_mtx);
    while (empty())
    {
        _cond.wait(lk);
    }

    std::unique_ptr<Task> p = std::move(_tasks.front());

    _tasks.pop();
    _cond.notify_all();

    return p;
}

bool TaskQueue::empty() const
{
    return _tasks.size() == 0;
}

bool TaskQueue::full() const
{
    return _tasks.size() == _queSize;
}

size_t TaskQueue::size() const
{
    return _tasks.size();
}